/*
 * GeneticAlgorithmParameters.cpp
 *
 *  Created on: Oct 21, 2013
 *      Author: filipe
 */
#include <cstdio>
#include <cstdlib>
#include "GeneticAlgorithmParameters.h"

namespace EvolutionaryOptimization
{
	// TODO: put the parameters in the same order that it is in the other constructors

	void GeneticAlgorithmParameters::_GenericConstructor(int elite_size, int population_size, int dimension)
	{
		_EliteSize = 1;
		_Dimension = dimension;
		_PopulationSize = population_size;

		_MaxPermittedValues = (double *) calloc (_Dimension, sizeof(double));
		_MinPermittedValues = (double *) calloc (_Dimension, sizeof(double));

		for (int i = 0; i < _Dimension; i++)
		{
			_MinPermittedValues[i] = 0.0;
			_MaxPermittedValues[i] = 1.0;
		}
	}

	GeneticAlgorithmParameters::GeneticAlgorithmParameters(int dimension, int population_size)
	{
		_GenericConstructor(1, population_size, dimension);
	}


	GeneticAlgorithmParameters::GeneticAlgorithmParameters(int dimension, int population_size, int elite_size)
	{
		if (_EliteSize > _PopulationSize)
			exit(printf("Error: Elite Size cannot be bigger than Population Size\n"));

		_GenericConstructor(elite_size, population_size, dimension);
	}


	GeneticAlgorithmParameters::~GeneticAlgorithmParameters()
	{
		free(_MaxPermittedValues);
		free(_MinPermittedValues);
	}


	int GeneticAlgorithmParameters::GetDimension() const
	{
		return _Dimension;
	}


	int GeneticAlgorithmParameters::GetEliteSize() const
	{
		return _EliteSize;
	}


	int GeneticAlgorithmParameters::GetPopulationSize() const
	{
		return _PopulationSize;
	}

	double GeneticAlgorithmParameters::GetMaxValue(int dimesion) const
	{
		return _MaxPermittedValues[dimesion];
	}

	double GeneticAlgorithmParameters::GetMinValue(int dimesion) const
	{
		return _MinPermittedValues[dimesion];
	}

	void GeneticAlgorithmParameters::SetDimension(int dimension)
	{
		_Dimension = dimension;
	}


	void GeneticAlgorithmParameters::SetEliteSize(int elite_size)
	{
		_EliteSize = elite_size;

		if (_EliteSize > _PopulationSize)
			exit(printf("Error: Elite Size cannot be bigger than Population Size\n"));
	}


	void GeneticAlgorithmParameters::SetPopulationSize(int population_size)
	{
		_PopulationSize = population_size;
	}


	void GeneticAlgorithmParameters::SetMaxValue(int dimesion, double value)
	{
		if ((dimesion < 0) || (dimesion > _Dimension))
			exit(printf("Error: Trying to access invalid dimension\n"));

		_MaxPermittedValues[dimesion] = value;
	}

	void GeneticAlgorithmParameters::SetMinValue(int dimesion, double value)
	{
		if ((dimesion < 0) || (dimesion > _Dimension))
			exit(printf("Error: Trying to access invalid dimension\n"));

		_MinPermittedValues[dimesion] = value;
	}
}
